﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Swift;
using UniLua;

public class MonsterSign : MonoBehaviour {

    public Text Hp;
    public Transform Buff;
    public Text NameBoard;

    LuaMonster m = null;
    Dictionary<LuaTable, GameObject> tb2BuffTex = new Dictionary<LuaTable, GameObject>();
    public void SetMonsterInfo(LuaMonster mon)
    {
        m = mon;
        Refresh();
    }

    public void Refresh()
    {
        Hp.text = m.GetAttrInt("hp").ToString();
        NameBoard.text = m.Name;

        var buffs = m.Buffs;

        foreach (var b in buffs)
        {
            if (!tb2BuffTex.ContainsKey(b))
            {
                var go = Instantiate(Buff.Find("BuffText").gameObject);
                go.SetActive(true);
                go.transform.SetParent(Buff);
                tb2BuffTex[b] = go;
            }

            tb2BuffTex[b].GetComponent<Text>().text = b.GetStr("name").V.SValue().Substring(0, 1) + ":" + (int)b.GetStr("value").V.NValue;
        }

        foreach (var b in tb2BuffTex.Keys)
        {
            if (buffs.FirstIndexOf(b) < 0)
                Destroy(tb2BuffTex[b].gameObject);
        }
    }
}
